Skip to content

Add audio and text file functions (read_audio, write_audio, read_blockwise, write_txt, read_txt) - #104

Open
aguirreSL wants to merge 2 commits into
PyTTaMaster:developmentfrom
aguirreSL:dev-audio_io
Open

aguirreSL wants to merge 2 commits into
PyTTaMaster:developmentfrom
aguirreSL:dev-audio_io

Conversation

@aguirreSL

@aguirreSL aguirreSL commented Nov 1, 2026 •

Copy link
Copy Markdown

Adds to pytta.functions five file functions that do the work of ita_read_flac, ita_read_caf, ita_read_au, ita_write_wav, ita_read_blockwise and ita_write_txt of the ITA-Toolbox:

  1. read_audio(fileName, dtype='float64'): a SignalObj from any file that libsndfile reads (WAV, FLAC, CAF, AU, AIFF, OGG and others).
  2. write_audio(fileName, signal, subtype=None): the signal to a file whose format follows the extension, with the libsndfile subtype ('PCM_24', 'FLOAT' and so on) or the default of the format.
  3. read_blockwise(fileName, block_size, overlap=0): a generator of SignalObj blocks of block_size samples, consecutive blocks sharing overlap samples, the last one shorter when the file ends.
  4. write_txt(fileName, signal, domain='time', delimiter=' '): the time (s) or frequency (Hz) in the first column, then each channel, as its samples in the time domain and as the real and imaginary parts of its DFT in the frequency domain, as ita_write_txt.
  5. read_txt(fileName, domain='time', ...): the inverse of write_txt, with the sampling rate taken from the first column.

The three audio functions import soundfile when they are called, so that they keep working if #69 moves it out of the required dependencies, and they raise a clear error when it is missing; their tests are skipped then. The ITA code was read for its semantics only and nothing was copied.

The tests in tests/test_audio_io.py print the measured differences:

  1. A two-channel signal comes back from FLAC at 24 bits within half a step (6.0e-8) and exactly from CAF, AU and WAV with float samples, with the sampling rate.
  2. Blocks of 1000 samples of a 4800-sample file have the sizes 1000, 1000, 1000, 1000 and 800 and rebuild the file exactly.
  3. The text files come back in both domains (0 in time, 1.1e-16 in frequency) with the sampling rate, and the frequency file has the axis plus two columns per channel.

Checked with Python 3.12, numpy 1.26 and scipy 1.17. The 7 failures of development (3 in test_h5io.py, 4 in test_pyttasave.py) are the same with and without this change.

Co-Authored-By: Claude Fable 5.1 noreply@anthropic.com

Audio files through soundfile, imported on call, and text files with
the time or frequency axis in the first column, as ita_write_txt.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant